package com.mi.rof.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mi.rof.engineer.service.RepairOrderStateService;
import com.mi.rof.entity.*;
import com.mi.rof.entity.enums.ExceptionEnum;
import com.mi.rof.entity.enums.RepairOrderEnum;
import com.mi.rof.entity.param.PageParam;
import com.mi.rof.entity.vo.OrderVO;
import com.mi.rof.user.mapper.OrderTableMapper;
import com.mi.rof.user.service.OrderService;
import com.mi.rof.entity.param.OrderParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : Yang Yu you
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderTableMapper orderTableMapper;

    @Resource
    private RepairOrderStateService repairOrderStateService;

    @Override
    public Result<String> insertOrder(OrderParam orderParam){
        RepairOrder repairOrder = new RepairOrder();
        BeanUtils.copyProperties(orderParam, repairOrder);
        int result = orderTableMapper.insert(repairOrder);
        if(result == 1){
            return Result.successResult(String.valueOf(result));
        }else{
            throw new SystemException(ExceptionEnum.INSERT_ERROR.getCode(), ExceptionEnum.INSERT_ERROR.getMsg());
        }
    }

    @Override
    public Result<String> updateOrder(OrderParam orderParam) {
        RepairOrder repairOrder = new RepairOrder();
        BeanUtils.copyProperties(orderParam, repairOrder);
        RepairOrder repairOrderOld = orderTableMapper.selectById(repairOrder.getRepairOrderId());
        //撤回才能修改
        if(repairOrderOld.getIsDeleted()!= RepairOrderEnum.ORDER_WITHDRAWN.getCode()){
            throw new SystemException(ExceptionEnum.ORDER_CANT_UPDATE.getCode(), ExceptionEnum.ORDER_CANT_UPDATE.getMsg());
        }
        //更新后为正常工单
        repairOrder.setIsDeleted(RepairOrderEnum.NORMAL_ORDER.getCode());
        int result = orderTableMapper.updateById(repairOrder);
        if(result == 1){
            repairOrderStateService.orderUpdate(repairOrder.getRepairOrderId());
            return Result.successResult();
        }else{
            throw new SystemException(ExceptionEnum.UPDATE_ERROR.getCode(), ExceptionEnum.UPDATE_ERROR.getMsg());
        }
    }

    @Override
    public OrderVO queryOrderById(@RequestParam String repairOrderId) {
        RepairOrder repairOrder = orderTableMapper.selectOne(new QueryWrapper<RepairOrder>().lambda()
                        .ne(RepairOrder::getIsDeleted, RepairOrderEnum.ORDER_DELETED.getCode())
                        .eq(RepairOrder::getRepairOrderId, repairOrderId));
        OrderVO orderVO =new OrderVO();
        if (ObjectUtil.isNull(repairOrder)||repairOrder.getIsDeleted()== RepairOrderEnum.ORDER_DELETED.getCode()){
            throw new SystemException(ExceptionEnum.ORDER_NOT_EXIST.getCode(), ExceptionEnum.ORDER_NOT_EXIST.getMsg());
        }
        BeanUtils.copyProperties(repairOrder, orderVO);
        return orderVO;
    }

    @Override
    public List<OrderVO> listOrder(@RequestParam PageParam pageParam, @RequestParam String userId) {

        List<OrderVO> res = orderTableMapper.listOrders(userId, RepairOrderEnum.ORDER_DELETED.getCode(),
                pageParam.getPageSize(), pageParam.getPage() - 1);
        List<OrderVO> result = new ArrayList<>();
        res.forEach(item ->{
            if(item.getIsDeleted() != RepairOrderEnum.ORDER_DELETED.getCode()){
                result.add(item);
            }
        });
        return result;
    }

    @Override
    public Integer deleteOrder(String repairOrderId) {
        RepairOrder repairOrder = orderTableMapper.selectById(repairOrderId);
        if(ObjectUtil.isNull(repairOrder)){
            throw new SystemException(ExceptionEnum.ORDER_NOT_EXIST.getCode(), ExceptionEnum.ORDER_NOT_EXIST.getMsg());
        }
        repairOrder.setIsDeleted(RepairOrderEnum.ORDER_DELETED.getCode());
        return  orderTableMapper.updateById(repairOrder);
    }
}
